package hypeerweb;

import org.omg.CORBA.portable.Delegate;

import states.NodeState;

public class Neighbor extends StandardNodeRole {

	public Neighbor(int id) {
		super(id);
	}
	public Neighbor(int id, int height){
		super(id,height);
	}

	@Override
	public void AddMe(Node node) {
		// create a copy of parent node
		int parentID = node.getWebId().getParentsValue();
		Node parent = new Node(parentID);
		
		Connections connections = delegate.getConnections();
		connections.removeDownPointer(parent);

		connections.addNeighbor(node, delegate.getWebId());

		delegate.updateHeight();

		if (connections.getDownPointers().size() > 0) {
			delegate.setState(NodeState.DownPointingNodeState);
		} else {
			delegate.setState(NodeState.StandardNodeState);
		}
	}


	@Override
	public void ReplaceMe(Node node) {
		Connections connections = delegate.getConnections();
		connections.updateNeighbor(node);
	}

	@Override
	public void DeleteMe(Node node) {
		Connections connections = delegate.getConnections();
		Node parent = connections.getSmallestNeighborWithChild();
		if(delegate.compareTo(parent)==0){
			connections.setInverseSurrogateFold(node.getFold());
		}else{
			delegate.addDownPointer(parent);
		}
		connections.removeNeighbor(node);
	}

}
